package cn.iocoder.yudao.module.game.service.gameHistory;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.gameHistory.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameHistory.GameHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.gameHistory.GameHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 游戏日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GameHistoryServiceImpl implements GameHistoryService {

    @Resource
    private GameHistoryMapper historyMapper;

    @Override
    public Long createHistory(GameHistorySaveReqVO createReqVO) {
        // 插入
        GameHistoryDO history = BeanUtils.toBean(createReqVO, GameHistoryDO.class);
        historyMapper.insert(history);
        // 返回
        return history.getId();
    }

    @Override
    public void updateHistory(GameHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateHistoryExists(updateReqVO.getId());
        // 更新
        GameHistoryDO updateObj = BeanUtils.toBean(updateReqVO, GameHistoryDO.class);
        historyMapper.updateById(updateObj);
    }

    @Override
    public void deleteHistory(Long id) {
        // 校验存在
        validateHistoryExists(id);
        // 删除
        historyMapper.deleteById(id);
    }

    private void validateHistoryExists(Long id) {
        if (historyMapper.selectById(id) == null) {
            throw exception(HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public GameHistoryDO getHistory(Long id) {
        return historyMapper.selectById(id);
    }

    @Override
    public GameHistoryDO getHistory(Wrapper wrapper) {
        return historyMapper.selectOne(wrapper);
    }


    @Override
    public PageResult<GameHistoryDO> getHistoryPage(GameHistoryPageReqVO pageReqVO) {
        return historyMapper.selectPage(pageReqVO);
    }

}